package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.DataService;
import com.ivanceras.client.DataServiceAsync;
import com.ivanceras.shared.bo.Ad_tab;
import com.ivanceras.shared.bo.Ad_table;
import com.ivanceras.shared.bo.Ad_window;
import com.ivanceras.shared.db.DAO;
import com.ivanceras.shared.db.SerializableDAO;

public class AWindow extends Composite {
	private final DataServiceAsync dataService = GWT
	.create(DataService.class);

	private static AWindowUiBinder uiBinder = GWT.create(AWindowUiBinder.class);

	interface AWindowUiBinder extends UiBinder<Widget, AWindow> {
	}
	
	@UiField TabPanel tabPanel;
	@UiField DivElement windowName;
	List<Ad_table> tableList = new ArrayList<Ad_table>();
	List<ATab> tabList = new ArrayList<ATab>();
	
	public AWindow(Ad_window window) {
		initWidget(uiBinder.createAndBindUi(this));
		windowName.setInnerText(window.getName());
		tabPanel.addSelectionHandler(new SelectionHandler<Integer>() {
			
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				int index = event.getSelectedItem();
				retrieveTableData(tableList.get(index), tabList.get(index), 1, 1);
			}

		});
		retrieveTabList(window);
	}
	
	void retrieveTabList(Ad_window window){
		dataService.retriveTabList(window, new AsyncCallback<Ad_tab[]>(){

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Ad_tab[] result) {
				for(final Ad_tab t: result){
					ATab tab = new ATab(t);
					tabList.add(tab);
					Ad_table table = t.getAd_table();
					tableList.add(table);
					tabPanel.add(tab, t.getName());
				}
				tabPanel.selectTab(0);
			}
			
		});
	}
	
	private void retrieveTableData(final Ad_table table, final ATab tab, Integer page, Integer itemPerPage) {
		System.out.println("Retrieving from table: "+table.getName());
		
		//create a lightweight version to be used as RPC param
		Ad_table ptable = new Ad_table();
		ptable.setAd_table_id(table.getAd_table_id());
		ptable.setTablename(table.getTablename());

		dataService.retrieveTableData(ptable, page, itemPerPage, null, null, new AsyncCallback<SerializableDAO[]>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(SerializableDAO[] result) {
				DAO[] daoList = SerializableDAO.cast(result);
				if(daoList != null && daoList.length > 0){
					tab.setDAOContent(daoList, 0);
				}
			}
		});
	}

}
